package hanjia;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.Set;

/**
 * Description:
 * Author:    Dane
 * CreateTime:2025/2/11-下午10:13
 * Since  :   1.0
 * Version:   1.0
 */
public class LeetCode501 {
    public static int[] findMode(TreeNode root) {
        ArrayList<Integer> result = new ArrayList<>();
        inOrder(root,result);
        HashMap<Integer, Integer> map = new HashMap<>();
        for (int i:result){
            map.put(i,map.getOrDefault(i,0)+1);
        }
        //元素
        int max=0;
        int maxCount=0;

        Set<Integer> keySet = map.keySet();
        for (int i:keySet){
            if (map.get(i)>maxCount){
                max=i;
                maxCount=map.get(i);
            }
        }
        ArrayList<Integer> temp = new ArrayList<>();
        for (int i:keySet){
            if (map.get(i)==maxCount){
              temp.add(i);
            }
        }
        int[] finalResult = new int[temp.size()];
        for (int i = 0; i < finalResult.length; i++) {
            finalResult[i]=temp.get(i);
        }
        return finalResult;


    }

    private static void inOrder(TreeNode root, ArrayList<Integer> result) {

        if (root==null){
            return;
        }

        inOrder(root.left,result);
        result.add(root.val);
        inOrder(root.right,result);


    }

    public static void main(String[] args) {
        TreeNode treeNode = new TreeNode(0);
        findMode(treeNode);
    }
}
